import execjs,codecs,random,time,os
from PIL import Image
from io import BytesIO
from aip import AipOcr

def get_rsa_pwd(pwd):
    with open("./common/js/security.js", 'r') as f:
    #with codecs.open("./common/js/security.js", 'r', 'utf-8') as f:
        content = f.read() #读取js文件的全部内容到content变量中
    ctx = execjs.compile(content)
    jscode = 'rsaEn("{0}")'.format(pwd) #js代码赋值
    res = ctx.eval(jscode) #执行js代码
    return res

def rand():
    return random.randint(7000,100000)

#def download_validatecode(res):
#    with open('./common/download/validate.png', 'wb') as f:
#        for data in res.iter_content(128):
#            f.write(data)
def get_img_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
def download_validatecode(res):
    APP_ID = '15476638'
    API_KEY = 'YhoiS6SstojZCb6gTBW1Eugw'
    SECRET_KEY = 'O9Lj9EBOrirE6QaEx5pGfj9xieI1ey1M'
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    i = Image.open(BytesIO(res.content))
    path=os.path.join(os.getcwd(),'common/download/validate.jpg')
    i.save(path)
    img=get_img_content(path)
    #result = client.basicAccurate(img)
    result = client.basicGeneral(img)
    time.sleep(2)
    res.imgnum=result['words_result'][0]['words']
